"""
删除并重建chapters表
"""
import asyncio
from tortoise import Tortoise
from tool.sessing import TORTOISE_ORM

async def recreate():
    await Tortoise.init(config=TORTOISE_ORM)
    
    # 获取数据库连接
    conn = Tortoise.get_connection("default")
    
    # 先删除外键约束
    try:
        await conn.execute_query("ALTER TABLE `file_storage` DROP FOREIGN KEY `fk_file_sto_chapters_8f77d383`")
        print("[OK] 已删除外键约束")
    except Exception as e:
        print(f"[INFO] 外键约束: {e}")
    
    # 删除旧表
    await conn.execute_query("DROP TABLE IF EXISTS `chapters`")
    print("[OK] 已删除旧的chapters表")
    
    # 重新生成schema
    await Tortoise.generate_schemas()
    print("[OK] 已重建chapters表（包含content字段）")
    
    await Tortoise.close_connections()

asyncio.run(recreate())

